package cinvestav.adviseme.webservice;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import cinvestav.adviseme.controller.ComentarioController;
import cinvestav.adviseme.datamodel.Comentario;
import cinvestav.adviseme.datamodel.Lugar;
import cinvestav.adviseme.datamodel.Persona;
import cinvestav.adviseme.util.DBException;
import cinvestav.adviseme.util.DataBaseDictionary;
import cinvestav.adviseme.util.QueryParamDictionary;

@Path("/comentario")
public class ComentarioWebService {
	protected ComentarioController comentarioController;
	
	public ComentarioWebService() throws DBException{				
		comentarioController = new ComentarioController();
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/getComments")
	public List<Comentario> getsComments(@QueryParam(QueryParamDictionary.LUGAR_ID) String id_lugar){
		Map<String, String[]> mapaConsulta = new HashMap<String, String[]>();
		String[] valor = new String[2];

		valor[0] = id_lugar;
        valor[1] = DataBaseDictionary.COLUMN_TYPE_COMENTARIO_ID_LUGAR;
        mapaConsulta.put(DataBaseDictionary.COLUMN_FILTRO_NAME_COMENTARIO_ID_LUGAR, valor);
		System.out.println("id_lugar = "+id_lugar);		
		try {
			List<Comentario> comentarios = new LinkedList<Comentario>();
			comentarios= comentarioController.getByFilters(mapaConsulta);
			if (comentarios.isEmpty()){            	
            	comentarios.add(new Comentario());
            }
			return comentarios;
		} catch (DBException e) {
			// TODO: handle exception
			System.out.println("id_lugar = "+e.getMessage());
			Logger.getLogger(LugarWebService.class.getName()).log(Level.SEVERE,null,e);
			throw new WSErrorException(e.getMessage());			
		}		
	}
	
	@POST	
	@Path("/insertComments")	
	public void insertComments(JSONObject commentObject){
		try {
			JSONObject jsonComment;
			JSONObject jsonPersona;
			JSONObject jsonLugar;
			
			Comentario comment = new Comentario();
			Persona persona = new Persona();
			Lugar lugar = new Lugar();
			
			try {
				Logger.getLogger(ComentarioWebService.class.getName()).log(
						Level.INFO,
						"request = " + commentObject.toString());
				jsonComment = new JSONObject(commentObject.getString(QueryParamDictionary.COMENTARIO_PARAM));				
				comment.setComentario(jsonComment.getString("comentario"));
				comment.setRanking(jsonComment.getInt("ranking"));
				comment.setFecha(jsonComment.getString("fecha"));
				jsonPersona = jsonComment.getJSONObject("persona");
				persona.setId(jsonPersona.getInt("id"));
				comment.setPersona(persona);
				jsonLugar = jsonComment.getJSONObject("lugar");
				lugar.setId(jsonLugar.getInt("id"));
				comment.setLugar(lugar);
				comentarioController.insert(comment);								
			} catch (JSONException e) {
				// TODO: handle exception
				Logger.getLogger(ComentarioWebService.class.getName()).log(Level.SEVERE,null,e);
				throw new WSErrorException("PARAMETROS INCORRECTOS");
			}
			
		} catch (DBException e) {
			// TODO: handle exception
			Logger.getLogger(ComentarioWebService.class.getName()).log(
					Level.SEVERE, null, e);
			throw new WSErrorException(e.getMessage());
		}
	}
	

}
